package com.wyy.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyy.error.BusinessException;
import com.wyy.error.EnumBusinessError;
import com.wyy.mapper.ArticleMapper;
import com.wyy.mapper.CommentsMapper;
import com.wyy.mapper.LikeArticleMapper;
import com.wyy.mapper.UserMapper;
import com.wyy.pojo.Articles;
import com.wyy.pojo.Comments;
import com.wyy.pojo.LikeArticle;
import com.wyy.pojo.Users;
import com.wyy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-07 10:53
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @Autowired
    private LikeArticleMapper likeArticleMapper;


    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public Map<String, Object> getAllUsers(int curPage) {

        HashMap<String, Object> map = new HashMap<>();
        //分页，根据前端传过来的 p 页数
        Page<Users> page = new Page<>(curPage, 10);
        //总篇数
        long total;
        //总页数
        long pages;
        List<Users> users;

        Page<Users> usersPage = userMapper.selectPage(page, null);
        pages = usersPage.getPages();
        total = usersPage.getTotal();
        users = usersPage.getRecords();
        //总篇数
        map.put("total", total);
        //总页数
        map.put("pages", pages);
        //文章 <=10篇
        map.put("users", users);

        return map;
    }

    @Override
    public void updateUser(Users users) {
        int i = userMapper.updateUser(users);
        if (i <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "修改" + users.getUserNickname() + "失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteUser(Users users) {
        //本来这里想用一条sql语句删除的，发现实现不了
        //1、删除用户
        int i = userMapper.deleteUser(users);
        if (i <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "删除" + users.getUserNickname() + "失败");
        }
        //2、删除文章
        List<String> articleIds = userMapper.getUserArticleIdByUserId(users.getUserId());
        QueryWrapper<Articles> w1 = new QueryWrapper<>();
        w1.eq("user_id", users.getUserId());
        articleMapper.delete(w1);
        //3、删除点赞
        QueryWrapper<LikeArticle> w2 = new QueryWrapper<>();
        w2.eq("article_id", articleIds);
        likeArticleMapper.delete(w2);
        //4、删除评论
        QueryWrapper<Comments> w3 = new QueryWrapper<>();
        w3.eq("article_id", articleIds);
        commentsMapper.delete(w3);
        //5、删除自己的评论
        QueryWrapper<Comments> w4 = new QueryWrapper<>();
        w3.eq("user_id", users.getUserId());
        commentsMapper.delete(w4);
    }

    @Override
    public Map<String, Object> getUserByUserId(String userId) {
        String userId1 = JSON.parseObject(userId, Users.class).getUserId();
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId1);
        Users users = userMapper.selectOne(wrapper);
        if (users == null || !"admin".equals(users.getUserRole())) {
            throw new BusinessException(EnumBusinessError.USER_NOT_LOGIN, "用户未登录或者不是管理员");
        }
        users.setUserPassword("");
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", users);
        return map;
    }

    @Override
    public Map<String, Object> login(Users users) {
        Users selectOne = userMapper.login(users.getUserNickname(), users.getUserPassword());
        if (selectOne == null) {
            throw new BusinessException(EnumBusinessError.USER_NOT_LOGIN, "账号或密码错误或者不是管理员");
        }
        selectOne.setUserPassword("");
        HashMap<String, Object> map = new HashMap<>();
        map.put("login_user", selectOne);
        return map;
    }
}
